package com.shangs.platform.modular.cms.image.controller;

import com.shangs.platform.common.exception.BizExceptionEnum;
import com.shangs.platform.common.exception.BussinessException;
import com.shangs.platform.common.persistence.dao.CmsImageMapper;
import com.shangs.platform.common.persistence.model.CmsImage;
import com.shangs.platform.core.base.controller.BaseController;
import com.shangs.platform.core.log.LogObjectHolder;
import com.shangs.platform.core.shiro.ShiroKit;
import com.shangs.platform.core.support.BeanKit;
import com.shangs.platform.core.util.ToolUtil;
import com.shangs.platform.modular.cms.warpper.ImageWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 图片控制器
 *
 * @author shangs
 * @Date 2017-10-25 15:22:32
 */
@Controller
@RequestMapping("/image")
public class ImageController extends BaseController {

    private String PREFIX = "/cms/image/";

    @Resource
    CmsImageMapper cmsImageMapper;

    /**
     * 跳转到图片首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "image.html";
    }

    /**
     * 跳转到添加图片
     */
    @RequestMapping("/image_add")
    public String imageAdd() {
        return PREFIX + "image_add.html";
    }

    /**
     * 跳转到修改图片
     */
    @RequestMapping("/image_update/{imageId}")
    public String imageUpdate(@PathVariable Long imageId, Model model) {
        if (ToolUtil.isEmpty(imageId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CmsImage cmsImage = this.cmsImageMapper.selectByPrimaryKey(imageId);
        Map<String, Object> map = BeanKit.beanToMap(cmsImage);
        model.addAttribute("image", map);
        LogObjectHolder.me().set(map);
        return PREFIX + "image_edit.html";
    }

    /**
     * 获取图片列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = this.cmsImageMapper.selectByCondition(new CmsImage());
        return new ImageWarpper(list).warp();
    }

    /**
     * 新增图片
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CmsImage image) {
        image.setCreateId(ShiroKit.getUser().getId());
        image.setCreateTime(new Date());
        this.cmsImageMapper.insertSelective(image);
        return SUCCESS_TIP;
    }

    /**
     * 删除图片
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long imageId) {
        this.cmsImageMapper.deleteByPrimaryKey(imageId);
        return SUCCESS_TIP;
    }


    /**
     * 修改图片
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid CmsImage cmsImage, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        cmsImage.setUpdateId(ShiroKit.getUser().getId());
        cmsImage.setUpdateTime(new Date());
        this.cmsImageMapper.updateByPrimaryKeySelective(cmsImage);
        return SUCCESS_TIP;
    }

    /**
     * 图片详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(@RequestParam Long imageId) {
        if (ToolUtil.isEmpty(imageId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CmsImage cmsImage = this.cmsImageMapper.selectByPrimaryKey(imageId);
        Map<String, Object> map = BeanKit.beanToMap(cmsImage);
        return map;
    }
}
